package com.common.utils.tool;

import cn.hutool.core.util.StrUtil;
import com.common.utils.constant.Constants;

import java.util.HashMap;
import java.util.Map;

/**
 * 描述：
 * 文件的contentType工具类
 * @author xianggj
 * @Date 2021/9/1 16:49
 **/
public class ContentTypeUtil {

    private static final Map<String, String> contentTypeMap = new HashMap();

    private static final String DEFAULT_CONTENT_TYPE =  "text/xml;charset=utf-8" ;

    static {
        contentTypeMap.put("xls", "application/x-xls");
        contentTypeMap.put("xlsx", "application/x-xls");
        contentTypeMap.put("doc", "application/msword");
        contentTypeMap.put("docx", "application/msword");
        contentTypeMap.put("ppt", "application/x-ppt");
        contentTypeMap.put("pptx", "application/x-ppt");
        contentTypeMap.put("json", "application/json");
        contentTypeMap.put("py", "text/x-python");
        contentTypeMap.put("pdf", "application/pdf");
        contentTypeMap.put("jpg", "image/jpeg;charset=utf-8");
        contentTypeMap.put("jpe", "image/jpeg;charset=utf-8");
        contentTypeMap.put("jpeg", "image/jpeg;charset=utf-8");
        contentTypeMap.put("png", "image/png;charset=utf-8");
        contentTypeMap.put("gif", "image/gif;charset=utf-8");
        contentTypeMap.put("bmp", "application/x-bmp;charset=utf-8");
        contentTypeMap.put("ico", "image/x-icon;charset=utf-8");
        contentTypeMap.put("mp3", "audio/mp3;charset=utf-8");
        contentTypeMap.put("mp4", "video/mpeg4;charset=utf-8");
        contentTypeMap.put("avi", "video/avi;charset=utf-8");
        contentTypeMap.put("wmv", "video/x-ms-wmv");
        contentTypeMap.put("wma", "video/wma");
        contentTypeMap.put("w4a", "video/mp4");
        contentTypeMap.put("w4v", "video/mp4");
        contentTypeMap.put("wov", "video/quicktime");
        contentTypeMap.put("3gpp", "video/3gpp");
        contentTypeMap.put("webm", "video/webm");
        contentTypeMap.put("flv", "video/x-flv");
        contentTypeMap.put("mpeg", "video/mpg");
        contentTypeMap.put("mts", "video/vnd.dlna.mpeg-tts");
        contentTypeMap.put("vob", "video/vob");
        contentTypeMap.put("mkv", "video/x-matroska");
    }


    /**
     * 获取文件ContentType
     * @param key
     * @return
     */
    public static String getContentType(String key) {
        if (StrUtil.isBlank(key)){
            return  DEFAULT_CONTENT_TYPE;
        }
        key = StrUtil.subAfter(key, Constants.DOT, true);
        key = key.toLowerCase();
        if (contentTypeMap.containsKey(key)){
            return contentTypeMap.get(key);
        }
        return  DEFAULT_CONTENT_TYPE;

    }
}
